<?php

namespace app\model\user;

use app\base\BaseModel;
use app\libs\Util;
use think\model\concern\SoftDelete;

/**
 * 模型-UserMarketModel
 * Class UserMarketModel
 * @package app\model
 */
class UserMarketModel extends BaseModel
{
    use SoftDelete;

    protected $name = 'user_market';

    /**
     * 获取分页列表
     * @param $current_page
     * @param $list_rows
     * @param string $field
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function getPageData($current_page, $list_rows, $field = 'a.*,b.nick_name,b.head_img')
    {
        $dataList = $this->alias('a')
            ->join("t_user b", "a.user_id=b.id")
            ->field($field)
            ->order('create_time', 'desc')
            ->paginate(['page' => $current_page, 'list_rows' => $list_rows], false);
        $current = $dataList->currentPage();
        $total = $dataList->total();
        $limit = $dataList->listRows();
        $ret = ['total' => $total, 'current' => $current, 'limit' => $limit, 'dataList' => $dataList->items()];
        return $ret;
    }

    /**
     * @param $id
     * @param $status  状态 1审核中 2审核失败 3审核通过
     * @return bool
     */
    public static function changeMarket($id, $status)
    {
        $ret = self::where(['id' => $id])->update(['status' => $status]);
        if ($ret !== false) {
            return true;
        }
        return false;
    }

    /**
     * 是否是推广员
     * @param $user_id
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function isMarket($user_id)
    {
        $data = self::where(['user_id' => $user_id])->find();
        if (empty($data)) {
            return false;
        }
        if (intval($data['status']) == 3) {
            return true;
        }
        return false;
    }

    /**
     * 添加推广员申请
     * @param $user_id
     * @param $explain
     * @param $phone
     * @param $wechat
     * @return bool
     */
    public static function addMarket($user_id, $explain, $phone, $wechat)
    {
        $data = self::where(['user_id' => $user_id])->find();
        if (empty($data)) {
            $in_data = [
                'user_id' => $user_id,
                'explain' => $explain,
                'phone' => $phone,
                'wechat' => $wechat,
                'update_time' => time(),
                'create_time' => time(),
            ];
            $ret = self::insert($in_data);
        } else {
            $up_data = [
                'user_id' => $user_id,
                'explain' => $explain,
                'phone' => $phone,
                'wechat' => $wechat,
                'update_time' => time()
            ];
            $id = $data['id'];
            $ret = self::where(['id' => $id])->update($up_data);
        }
        if ($ret === false) {
            return false;
        }
        return true;
    }

    /**
     * 获取用户状态
     * @param $user_id
     * @return array|null|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getStatus($user_id)
    {
        $data = $this->where("id", $user_id)->field("status")->find();
        if (!empty($data)) {
            return $data['status'];
        }
        return 0;
    }

    /**
     * 设置用户登录存储信息
     * @param $data
     * @return mixed
     */
    public function setUserLoginStorage($data)
    {
        $flag = $this->redis->hSet($this->redis_key, $data['id'], serialize($data));
        $this->redis->expire($this->redis_key, $data['expires_in']);
        return $flag;
    }

    /**
     * 获取用户登录存储信息
     * @param $user_id
     * @return mixed
     */
    public function getUserLoginStorage($user_id)
    {
        $data = $this->redis->hGet($this->redis_key, $user_id);
        if ($data) {
            $data = unserialize($data);
        }
        return $data;
    }
}